import React from 'react';
import { Button, Form, Input,Modal } from 'antd';
import {CloseOutlined} from '@ant-design/icons';
import styles from './edit.module.scss';
import { req } from '@/common/methods';


const Edit = (props) => {
    const {ty,id,currentItem,doClose} = props;
    const isAdd = ty === 'add';
    const onFinish = values => {
        req('/api/suppliers/edit',{...values},'post').then((data)=>{
            //console.log(data);
            Modal.success({
                title:'提交成功',
                content:<>已完成{(data && data.name)?JSON.stringify(data.name):'对此'}供货商的{isAdd?'添加':'修改'}<br/></>,
                onOk(){
                    doClose();
                }
            })
        }).catch((error)=>{
            Modal.error({
                title:'提交失败',
                content:<>{''+error}</>
            })
        })
    }
    const onFinishFailed = error => {
        //console.log(error);
        Modal.error({
            title:'提交失败',
            content:<>错误信息:{typeof error === 'object' ? JSON.stringify(error || {}) : (''+error)}</>
        });
    }    
    return <>
        <div className={styles['close-area']} onClick={()=>Modal.destroyAll()}>
            <CloseOutlined style={{fontSize:'30px'}} />
        </div>
        <Form
            name="wrap"
            labelCol={{ flex: '110px' }}
            labelAlign="left"
            labelWrap
            initialValues={currentItem}
            wrapperCol={{ flex: 1 }}
            colon={false}
            style={{ maxWidth: 600 }}
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
        >
            {!isAdd && <Form.Item label="惟一标识符" name="id" rules={[{ required: true,message:'惟一标识符不能为空，请检查程序' }]}>
                <Input disabled/>
            </Form.Item>}
            <Form.Item label="供货商名称" name="name" rules={[{ required: true,message:'请输入供货商的名称' }]}>
                <Input />
            </Form.Item>
            <Form.Item label="包含人员" name="includes">
                <Input />
            </Form.Item>
            <Form.Item label="联系方式" name="telephone">
                <Input />
            </Form.Item>
            <Form.Item label=" ">
            <Button type="primary" htmlType="submit">
                提交
            </Button>
            </Form.Item>
        </Form>
    </>
}

export default Edit;